package com.capri.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import com.capri.utils.ScreenUtils;

import androidx.annotation.Nullable;

/**
 * 仪表盘View
 */
public class DashBoard extends View {

    private static int RADIUS=0;
    //外部画圆环的画笔
    private Paint mPaint;
    //画刻度的画笔
    private Paint mScalePaint;
    //画进度的画笔
    private Paint mProgressPaint;

    private Paint mServicePaint;

    private Paint mServiceTimePaint;

    //仪表盘进度值
    private float percent=0.0f;

    private String nameService="";

    private String serviceTime="_ _:_ _";

    public DashBoard(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();//初始化Paint
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int min=getWidth()/2>getHeight()/2?getHeight()/2:getWidth()/2;
        RADIUS=min;
        RADIUS=RADIUS-ScreenUtils.dp2px(getContext(),10);
        drawArc(canvas);
        setPercent(percent,canvas);
        setServiceName(canvas);
        setServiceTimeText(canvas);
    }

    private void initPaint() {
        int min=getWidth()/2>getHeight()/2?getHeight()/2:getWidth()/2;
//        RADIUS=ScreenUtils.dp2px(getContext(),70);
        RADIUS=min;
        mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mPaint.setColor(Color.parseColor("#EFF3F6"));
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(ScreenUtils.dp2px(getContext(),10));

        mScalePaint=new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mScalePaint.setStyle(Paint.Style.STROKE);//画线模式
        mScalePaint.setStrokeWidth(ScreenUtils.dp2px(getContext(),2));//线宽度
        mScalePaint.setColor(Color.BLACK);

        mProgressPaint=new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
//        mProgressPaint.setColor(Color.RED);
        //设置渐变的蒙版
        int[] colors = {Color.parseColor("#81d4ff"), Color.parseColor("#008dff")};
        SweepGradient sweepGradient = new SweepGradient(0, 0, colors, null);
        mProgressPaint.setShader(sweepGradient);
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setStrokeWidth(ScreenUtils.dp2px(getContext(),10));

        //服务名称画笔
        mServicePaint=new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mServicePaint.setColor(Color.parseColor("#000000"));
        mServicePaint.setTextSize(ScreenUtils.sp2px(getContext(),10));

        mServiceTimePaint=new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mServiceTimePaint.setColor(Color.parseColor("#2995ED"));
        mServiceTimePaint.setTextSize(ScreenUtils.sp2px(getContext(),20));
    }

    /**
     * 画表盘
     * @param canvas
     */
    private void drawArc(Canvas canvas){
        RectF rectF=new RectF(getWidth()/2-RADIUS,getHeight()/2-RADIUS,getWidth()/2+RADIUS,getHeight()/2+RADIUS);
        canvas.drawArc(rectF,-210,240,false,mPaint);
    }

    /**
     * 画刻度
     */
    private void rotateCanvasDrawScale(Canvas canvas){
        //平移到左上角的原点位置
        canvas.translate(getWidth()/2,getHeight()/2);
        canvas.rotate(30);
        for (int i=0;i<20;i++){
            canvas.drawLine(RADIUS-20-ScreenUtils.px2dp(getContext(),10),0,RADIUS-20,0,mScalePaint);
            canvas.rotate(-240/20);//逆时针选择 负值是逆时针
        }
        //最后一根线
        canvas.drawLine(RADIUS-20-ScreenUtils.px2dp(getContext(),10),0,RADIUS-20,0,mScalePaint);
        canvas.rotate(240-30);//旋转回去的角度
        canvas.translate(-getWidth()/2,-getHeight()/2);
    }

    /**
     * 画进度方法
     * @param percent
     */
    private void setPercent(float percent,Canvas canvas){
        RectF rectF=new RectF(getWidth()/2-RADIUS,getHeight()/2-RADIUS,getWidth()/2+RADIUS,getHeight()/2+RADIUS);
        canvas.drawArc(rectF,-210,240*percent,false,mProgressPaint);
    }

    public void setPercentUser(float p){
        percent=p;
        invalidate();
    }

    /**
     * 服务名称
     */
    private void setServiceName(Canvas canvas){
        Rect mBound = new Rect();
        mServicePaint.getTextBounds(nameService, 0, nameService.length(), mBound);
        canvas.drawText(nameService,getWidth()/2-mBound.width()/2,getHeight()/3,mServicePaint);
    }

    public void setServiceNameUser(String serviceName){
        nameService=serviceName;
        invalidate();
    }

    private void setServiceTimeText(Canvas canvas){
        Rect mBound = new Rect();
        mServiceTimePaint.getTextBounds(serviceTime, 0, serviceTime.length(), mBound);
        canvas.drawText(serviceTime,getWidth()/2-mBound.width()/2,getHeight()/2,mServiceTimePaint);
    }

    public void setServiceTimeTextUser(String time){
        serviceTime=time;
        invalidate();
    }
}
